package com.ruoyi.system.Fdongbei.service.impl;

import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.system.Fdongbei.domain.TbGuozhong;
import com.ruoyi.system.Fdongbei.domain.TbSuoshugongsi;
import com.ruoyi.system.Fdongbei.domain.TbWx;
import com.ruoyi.system.Fdongbei.mapper.TbGuozhongMapper;
import com.ruoyi.system.Fdongbei.mapper.TbMenjinMapper;
import com.ruoyi.system.Fdongbei.mapper.TbSuoshugongsiMapper;
import com.ruoyi.system.Fdongbei.mapper.TbWxMapper;
import com.ruoyi.system.Fdongbei.service.ITbGuozhongService;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;


/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author fufeng
 * @since 2024-12-05
 */
@Service
public class TbGuozhongServiceImpl implements ITbGuozhongService {


    @Resource
    TbMenjinMapper tbMenjinMapper;

    @Resource
    TbGuozhongMapper tbGuozhongMapper;

    @Resource
    TbSuoshugongsiMapper tbSuoshugongsiMapper;

    @Resource
    TbWxMapper tbWxMapper;


    @Transactional(rollbackFor = Exception.class)
    @DataSource(value = DataSourceType.LY002_D)
    public int tbGuozhongInsert(TbGuozhong tbGuozhong) {
        String czbh = generateCZBH(tbGuozhong.getCzbh());
        tbGuozhong.setCzbh(czbh);
        tbGuozhong.setcard_id(czbh);
        tbSuoshugongsiMapper.insertTbsuoshugongsi(new TbSuoshugongsi(czbh, "东北"));
        tbWxMapper.insertWx(new TbWx(0, tbGuozhong.getLxdh(), czbh, tbGuozhong.getChehao()));

        return tbGuozhongMapper.tbGuozhonginsert(tbGuozhong);

    }

    @DataSource(value = DataSourceType.LY002_D)

    public int countguozhong(String ddbh) {
        return tbGuozhongMapper.countguozhong(ddbh);
    }

    @Transactional(rollbackFor = Exception.class)
    @DataSource(value = DataSourceType.LY002_D)
    public int tbGuozhonginsertsd(TbGuozhong tbGuozhong) {
        String czbh = generateCZBH(tbGuozhong.getCzbh());
        tbGuozhong.setCzbh(czbh);
        tbGuozhong.setcard_id(czbh);
        tbSuoshugongsiMapper.insertTbsuoshugongsi(new TbSuoshugongsi(czbh, "东北"));
        tbWxMapper.insertWx(new TbWx(0, tbGuozhong.getLxdh(), czbh, tbGuozhong.getChehao()));

        return tbGuozhongMapper.tbGuozhonginsertsd(tbGuozhong);

    }

    @DataSource(value = DataSourceType.LY002_D)
    public String tbGuozhongExists(String chehao) {
        int a = tbGuozhongMapper.tbGuozhongExists(chehao);
        if (a != 0) {
            return "存在未出场记录";
        }
        if (tbMenjinMapper.carRecordExists(chehao) != 0) {
            return "存在门禁未出场记录";
        }
        return "OK";
    }

    //生成czbh传入LR 或LC
    @DataSource(value = DataSourceType.LY002_D)
    public String generateCZBH(String gsbh) {
        LocalDate localDate = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyMMdd");
        String formattedDate = localDate.format(formatter);
        String czbhmax = tbGuozhongMapper.selectMaxCzbh(localDate + " 00:00:00", localDate + " 23:59:59");
        if (czbhmax != null) {
            DecimalFormat decimalFormat = new DecimalFormat("0000"); // 4 位宽度
            String formattedString = decimalFormat.format(Integer.parseInt(czbhmax) + 1);
            return gsbh + formattedDate + formattedString;
        } else {
            return gsbh + formattedDate + "0001";
        }

    }


    @Transactional(rollbackFor = Exception.class)
    @DataSource(value = DataSourceType.LY002_D)
    public int tbGuozhonginsertsh(TbGuozhong tbGuozhong) {
        String czbh = generateCZBH(tbGuozhong.getCzbh());
        tbGuozhong.setCzbh(czbh);
        tbGuozhong.setcard_id(czbh);
        tbSuoshugongsiMapper.insertTbsuoshugongsi(new TbSuoshugongsi(czbh, "东北"));
        tbWxMapper.insertWx(new TbWx(0, tbGuozhong.getLxdh(), czbh, tbGuozhong.getChehao()));
        return tbGuozhongMapper.tbGuozhonginsertsh(tbGuozhong);

    }

    @Transactional(rollbackFor = Exception.class)
    @DataSource(value = DataSourceType.LY002_D)
    public int tbGuozhonginsertzz(TbGuozhong tbGuozhong) {
        String czbh = generateCZBH(tbGuozhong.getCzbh());
        tbGuozhong.setCzbh(czbh);
        tbGuozhong.setcard_id(czbh);
        tbSuoshugongsiMapper.insertTbsuoshugongsi(new TbSuoshugongsi(czbh, "东北"));
        tbWxMapper.insertWx(new TbWx(0, tbGuozhong.getLxdh(), czbh, tbGuozhong.getChehao()));
        return tbGuozhongMapper.tbGuozhonginsertzz(tbGuozhong);

    }

    @DataSource(value = DataSourceType.LY002_D)
    public List<TbGuozhong> selectyccl(String sql) {
        return tbGuozhongMapper.selectyccl(sql);
    }


    @DataSource(value = DataSourceType.LY002_D)

    public TbGuozhong selectByCzbh(String czbh) {
        return tbGuozhongMapper.selectByCzbh(czbh);
    }

    @DataSource(value = DataSourceType.LY002_D)
    public int deleteguozhongbyczbh(String czbh) {
        return tbGuozhongMapper.deleteguozhongbyczbh(czbh);
    }

    @Transactional(rollbackFor = Exception.class)
    @DataSource(value = DataSourceType.LY002_D)
    public int deletedaijinchang(String czbh) {
        int a;
        String gbzt = tbGuozhongMapper.selectByCzbh(czbh).getGbzt();
        if ("待进厂".equals(gbzt) || "已进厂".equals(gbzt)) {
            a = tbGuozhongMapper.deleteguozhongbyczbh(czbh);
            a += tbSuoshugongsiMapper.deletessgsbyczbh(czbh);
            if ("已进厂".equals(gbzt)) {
                a += tbMenjinMapper.deletemenjinbyczbh(czbh);
            }
        } else {
            a = -1;
        }

        return a;

    }


    @DataSource(value = DataSourceType.LY002_D)
    public String updategbztbyczbh(String czbh) {
        String currentGbzt = tbGuozhongMapper.selectByCzbh(czbh).getGbzt();
        if (currentGbzt == null) {
            return "未查询到数据"; // 如果状态为 null，直接返回 -1
        }
        String gbzt;
        switch (currentGbzt) {
            case "进厂称重":
                gbzt = "已进场";
                break;
            case "卸车审核":
                gbzt = "进厂称重";
                break;
            case "卸车确认":
                gbzt = "卸车确认";
                break;
            default:
                return "为查询到合适状态"; // 如果状态不符合预期，直接返回 -1
        }
        if (tbGuozhongMapper.updategbztbyczbh(gbzt, czbh) > 0) {
            return "更新成功";
        } else {
            return "更新失败";
        } // 更新状态

    }

    @DataSource(value = DataSourceType.LY002_D)
    public String updatetuiche(String czbh) {
        TbGuozhong tbGuozhong = tbGuozhongMapper.selectByCzbh(czbh);
        if ("玉米".equals(tbGuozhong.getWlmc())) {
            return "物料不是玉米";
        }
        if ("正常".equals(tbGuozhong.getYj())) {
            tbGuozhongMapper.updateyjbyczbh("退车", czbh);
            return "";
        } else {
            tbGuozhongMapper.updateyjbyczbh("正常", czbh);
            return "";
        }
    }

    @DataSource(value = DataSourceType.LY002_D)
    public String zhuanyunbiaoji(String czbh, String gbzt) {
        if (gbzt.isEmpty()) {
            return "过磅状态为空";
        }
        if ("待进场".equals(gbzt)) {
            return "修改成功" + tbGuozhongMapper.updategbztbyczbh("已进场", czbh) + "条,修改成已进场";
        }
        if ("已进厂".equals(gbzt)) {
            return "修改成功" + tbGuozhongMapper.updategbztbyczbh("待进场", czbh) + "条,修改成待进厂";
        }
        return "状态不对";
    }

    @DataSource(value = DataSourceType.LY002_D)
    public TbGuozhong selectByYmqybsm(String ymqybsm) {
        return tbGuozhongMapper.selectByYmqybsm(ymqybsm);
    }


    @DataSource(value = DataSourceType.LY002_D)
    public int updatehykz(String hykz, String ymqybsm) {
        return tbGuozhongMapper.updatehykz(hykz, ymqybsm);
    }

    @DataSource(value = DataSourceType.LY002_D)
    public int updatejsdj(String jsdj, String orclgl, String ymqybsm) {
        return tbGuozhongMapper.updatejsdj(jsdj, orclgl, ymqybsm);
    }


}